﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Threading;

namespace D6.DFS.DevUtility
{
    public partial class ConnectionForm : Form
    {
        private string _typicalQueryConnStr = null;
        private string _dfsQueryConnStr = null;
        private MethodInvoker _updateUrlBox = null;
        private string _serverName = string.Empty;
        private string _portNumber = string.Empty;
        private MainForm _mainForm = null;

        public ConnectionForm(MainForm mainForm)
        {
            InitializeComponent();

            _typicalQueryConnStr = "http://[servername]:[portnumber]/services/core/QueryService";
            _dfsUrlTextBox.Text = _typicalQueryConnStr;
            _dfsQueryConnStr = _typicalQueryConnStr;
            _updateUrlBox = delegate {
                Monitor.Enter(_dfsUrlTextBox);
                _dfsUrlTextBox.Text = _dfsQueryConnStr;
                _dfsUrlTextBox.Invalidate();
                Monitor.Exit(_dfsUrlTextBox);
            };
            _mainForm = mainForm;
        }

        private void serverNameChanged(Object sender, KeyPressEventArgs args)
        {
            _serverName += args.KeyChar.ToString();
            _dfsQueryConnStr = _typicalQueryConnStr.Replace("[servername]", _serverName);
            _dfsQueryConnStr = _dfsQueryConnStr.Replace("[portnumber]", _portNumber);
            _dfsUrlTextBox.BeginInvoke(_updateUrlBox);
        }


        private void portNumberChanged(Object sender, KeyPressEventArgs args)
        {
            _portNumber += args.KeyChar.ToString();
            _dfsQueryConnStr = _typicalQueryConnStr.Replace("[servername]", _serverName);
            _dfsQueryConnStr = _dfsQueryConnStr.Replace("[portnumber]", _portNumber);
            _dfsUrlTextBox.BeginInvoke(_updateUrlBox);
        }

        private void connectButtonClicked(object sender, EventArgs e)
        {
            schemaRepositories.DfsRepositoryInfoRow dfsRow = AppWideData.Instance.DfsUrls.NewDfsRepositoryInfoRow();
            dfsRow.DFSURL = _dfsQueryConnStr;
            dfsRow.Username = _usernameTxtBox.Text;
            dfsRow.Password = _passwordTxtBox.Text;
            dfsRow.Repository = _repositoryTxtBox.Text;
            AppWideData.Instance.DfsUrls.AddDfsRepositoryInfoRow(dfsRow);
            AppWideData.Instance.CurrentDfsKey = dfsRow.DfsKey + 1;
            _mainForm.AddDfsConnection(AppWideData.Instance.CurrentDfsKey);
            Close();
        }

        private void _previewButton_Click(object sender, EventArgs e)
        {
            string servername = _serverNameTextBox.Text;
            _dfsQueryConnStr = _typicalQueryConnStr.Replace("[servername]", servername);
            string portnumber = _portNumberTextBox.Text;
            _dfsQueryConnStr = _dfsQueryConnStr.Replace("[portnumber]", portnumber);
            _dfsUrlTextBox.BeginInvoke(_updateUrlBox);
        }
    }
}
